package beautiful.butterfly.drds.datasource.connection_contexts.mock;

import beautiful.butterfly.drds.datasource.connection_contexts.api.IConnectionContext;
import beautiful.butterfly.drds.datasource.connection_contexts.api.IParameterizedISql;
import beautiful.butterfly.drds.datasource.connection_contexts.api.ISql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;


public class ConnectionContext implements IConnectionContext {


    private Connection connection = null;

    public ConnectionContext(Connection connection) {
        this.connection = connection;
    }

    public void close() throws SQLException {
        this.connection.close();
    }

    public boolean isClosed() throws SQLException {
        return this.connection.isClosed();
    }

    //
    public void setReadOnly(boolean readOnly) throws SQLException {
        this.connection.setReadOnly(readOnly);
    }

    public boolean isReadOnly() throws SQLException {
        return this.connection.isReadOnly();
    }

    public void setAutoCommit(boolean autoCommit) throws SQLException {
        this.connection.setAutoCommit(autoCommit);
    }

    public boolean getAutoCommit() throws SQLException {
        return this.connection.getAutoCommit();
    }

    public void commit() throws SQLException {
        this.connection.commit();
    }

    public void rollback() throws SQLException {
        this.connection.rollback();
    }

    //
    public ISql createSql() throws SQLException {
        Statement statement = this.connection.createStatement();
        Sql sql = new Sql(this, statement);
        return sql;
    }

    public IParameterizedISql createParameterizedSql(String sql) throws SQLException {
        PreparedStatement preparedStatement = this.connection.prepareStatement(sql);
        ParameterizedSql parameterizedSql = new ParameterizedSql(this, preparedStatement);
        return parameterizedSql;
    }

    public IParameterizedISql createParameterizedSql(String sql, int x) throws SQLException {
        PreparedStatement preparedStatement = this.connection.prepareStatement(sql);
        ParameterizedSql parameterizedSql = new ParameterizedSql(this, preparedStatement);
        return parameterizedSql;
    }


}
